+function($){
  var $body = $('body');
  $body.on('click', '.J_emupdate', function() {
    var emid = $(this).data('emid');
    var dialogContent = '<form id="emupdateForm" method="post"><ul>'+
        '<input type="hidden" name="shopid" id="shopid">'+
        '<li><laber class="list-laber">姓名：</laber><input name="emname" id="emname" class="list-input"></li>'+
        '<li><laber class="list-laber">品牌：</laber><input name="embrand" id="embrand" class="list-input"></li>'+
        '<li><laber class="list-laber">职位：</laber><input name="emposition" id="emposition" class="list-input"></li>'+
        '<li><laber class="list-laber">联系电话：</laber><input name="emtel" id="emtel" class="list-input"></li>'+
        '<li><laber class="list-laber">出生日期：</laber><div style="display:inline" class="YMD" data-ys="2017" data-ye="1880" id="embirth"> <select class="YYYY" name="birth-YYYY"> <option value="">请选择 年</option> </select> <select class="MM" name="birth-MM"> <option value="">选择 月</option> </select> <select class="DD" name="birth-DD"> <option value="">选择 日</option> </select> </div></li>'+
        '<li><laber class="list-laber">入职时间：</laber><div style="display:inline" class="YMD" data-ys="2017" data-ye="1880" id="emintime"> <select class="YYYY" name="intime-YYYY"> <option value="">请选择 年</option> </select> <select class="MM" name="intime-MM"> <option value="">选择 月</option> </select> <select class="DD" name="intime-DD"> <option value="">选择 日</option> </select> </div></li>'+
        '<li><laber class="list-laber">地址：</laber><input name="emaddr" id="emaddr" class="list-input"></li>'+
        '<div class="dialog-button"><button class="btn btn-sm btn-primary" data-emid="'+emid+'" id="emUpdate">修　改</button></div>'+
        '</ul></form>';
    var $this = $(this);
    deletedialog = dialog({
      skin: 'dialog-box',
      title:'温馨提示',
      content: dialogContent,
      width:'460px',
      skin:'  dialog-box',
      lock:true,
      padding:'10px'
    }).showModal(emupdateInit($this));
  });
  $body.on('click', '#emUpdate', function() {
    var flag = true;
      $('.list-input,.YYYY,.MM,.DD').each(function(index, el) {
        if($(this).val()==''){
          $(this).css('border', '3px solid #f77');
          flag = false;
          alert('请将信息补充完整');
          return false;
        }
        $(this).attr('style', ' ');
      });
      if(flag == false){
        return false;
      }
      var emintime = $('select[name="intime-YYYY"]').val()+'-'+$('select[name="intime-MM"]').val()+'-'+$('select[name="intime-DD"]').val();
      var embirth = $('select[name="birth-YYYY"]').val()+'-'+$('select[name="birth-MM"]').val()+'-'+$('select[name="birth-DD"]').val();
      var postdata = {
          emid:  $(this).data('emid'),
  				emname: $('#emname').val(),
  				embrand: $('#embrand').val(),
  				emtel: $('#emtel').val(),
  				emposition: $('#emposition').val(),
  				emintime: emintime,
  				embirth: embirth,
  				emaddr: $('#emaddr').val()
      }
			$.post('/employee/employee_update', postdata, function(data) {
        // console.log(data);
				if (data != 'false') {
					alert('修改成功');
				} else {
					alert('修改失败');
				}
        window.location.reload();
			});
      return false;
  });
  $body.on('click', '.J_emdelete', function() {
    var content = '<span style="color:#d00">确定要删除这一条记录吗？</span>'
                  '<div class="dialog-button"><button class="btn btn-sm btn-primary" id="callbackDelete" value="'+$(this).data('shopid')+'">确定</button><button style="margin-left:30px;" class="btn btn-sm btn-primary" id="closeDelete">取消</button></div>';
    deletedialog = dialog({
      title:'温馨提示',
      content: content,
      width:'300px',
      skin:'  dialog-box',
      lock:true,
      padding:'10px'
    }).showModal();
  });
  $body.on('click', '.J_summary', function() {
    var emid = $(this).data('emid');
    var emname = $(this).parent().prevAll('.J_emname').text()
    var dialogContent = '<ul>'+
        '<input type="hidden" name="shopid" id="shopid">'+
        '<li>员工：<span class="J_emname">'+emname+'</span></li>'+
        '<li>时间区间:<input id="stime" name="stime" type="text" class="Wdate" readonly>-<input id="otime" name="otime" type="text" class="Wdate" readonly></li>'+
        '<div class="dialog-button"><button style="margin-right:20px;" class="btn btn-sm btn-primary" data-emid="'+emid+'" id="alloutput">全部导出</button><button class="btn btn-sm btn-primary" data-emid="'+emid+'" id="output">按区间导出</button></div>'+
        '</ul>';
    var $this = $(this);
    deletedialog = dialog({
      skin: 'dialog-box',
      title:'导出员工总结',
      content: dialogContent,
      width:'450px',
      skin:'  dialog-box',
      lock:true,
      padding:'10px'
    }).showModal();
  });
  $body.on('click', '#alloutput', function(event) {
    window.location.href='/summary/summaryexcel?emid='+$(this).data('emid');
  });
  $body.on('click', '#output', function() {
    var flag = true;
    $('#stime,#otime').each(function(index, el) {
        if($(this).val()==''){
          $(this).css('border', '3px solid #f77');
					flag = false;
          alert('请将信息补充完整');
					return false;
        }
        $(this).attr('style', ' ');
    });
		if(flag == false){
			return false;
		}
    // var minday = $('select[name="intime-YYYY"]').val()+'-'+$('select[name="intime-MM"]').val()+'-'+$('select[name="intime-DD"]').val();
    // var maxday = $('select[name="birth-YYYY"]').val()+'-'+$('select[name="birth-MM"]').val()+'-'+$('select[name="birth-DD"]').val();
    window.location.href='/summary/summaryexcel?emid='+$(this).data('emid')+'&minday='+$('#stime').val()+'&maxday='+$('#otime').val();;
  });
  $('body').on('focus', '#stime', function() {
      WdatePicker({ skin: 'whyGreen', dateFmt: 'yyyy-MM-dd',maxDate:'#F{$dp.$D(\'otime\');}' });
  });
  $('body').on('focus', '#otime', function() {
      WdatePicker({ skin: 'whyGreen', dateFmt: 'yyyy-MM-dd',minDate:'#F{$dp.$D(\'stime\');}' });
  });
  $('body').on('click', '.J_resetupdate', function() {
    var content = '<span style="color:#d00;display:block;margin-bottom:10px;">是否要重置密码？</span>'+
                  '<div class="dialog-button"><button class="btn btn-sm btn-primary" id="resetpass" value="'+$(this).data('emid')+'">确定</button><button style="margin-left:30px;" class="btn btn-sm btn-primary" id="closeDelete">取消</button></div>';
    var $this = $(this);
    deletedialog = dialog({
      skin: 'dialog-box',
      title:'温馨提示',
      content: content,
      width:'250px',
      skin:'  dialog-box',
      lock:true,
      padding:'10px'
    }).showModal();
  });
  $('body').on('click', '#resetpass', function() {
    $.post('/employee/resetpass', {
      emid:$(this).val()
    }, function(data) {
      alert(data);
      location.reload(true);
    });
  });
  // var outputInit = function(){
  //   $('.YMD').ymd();
  // }
  //编辑信息初始化
  var emupdateInit = function($this){
    $('.YMD').ymd();
    var $that = $this.parent();
    $('#emname').val($that.prevAll('.J_emname').text());
    $('#embrand').val($that.prevAll('.J_embrand').text());
    $('#emposition').val($that.prevAll('.J_emposition').text());
    $('#emtel').val($that.prevAll('.J_emtel').text());
    $('#emaddr').val($that.prevAll('.J_emaddr').text());
    settime($that.prevAll('.J_embirth').text(),$('#embirth'));
    settime($that.prevAll('.J_emintime').text(),$('#emintime'));
  }
  //设置时间
  var settime = function(time,$){
    var time = time.split('-');
    $.children('.YYYY').val(time[0]);
    $.children('.MM').val(parseInt(time[1]));
    writeDay(time,$.children('.DD'));
    $.children('.DD').val(parseInt(time[2]));
  }
	var writeDay = function(time,e){
		var MonHead = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
    if(parseInt(time[1])==2 && IsPinYear(parseInt(time[0]))){
      var n = MonHead[1]+1;
    }else{
      var n = MonHead[parseInt(time[1])-1];
    }
		for (var i = 1; i < (n + 1); i++)
			e.append('<option value='+i+'>'+i+'日</option>');
	}
  var IsPinYear = function(year){
    return (0 == year % 4 && (year % 100 != 0 || year % 400 == 0));
  }
}(jQuery);
